//
//  LBSuperMarketHeaderView.m
//  loveBeen
//
//  Created by 韩天助 on 11/21/16.
//  Copyright © 2016 loveBeen. All rights reserved.
//

#import "LBSuperMarketHeaderView.h"
#import "LBNamePriceView.h"
#import "LBCaptureView.h"
#import "LBSafe_dayView.h"
#import "LBBrandView.h"
#import "LBSpecificsView.h"

@interface LBSuperMarketHeaderView()
@property(weak,nonatomic) UIImageView *iconView;
@property(weak,nonatomic) LBNamePriceView *namePriceV;
@property(weak,nonatomic) LBCaptureView *captureV;
@property(weak,nonatomic) LBBrandView *brandV;
@property(weak,nonatomic) LBSpecificsView *specificsV;
@property(weak,nonatomic) LBSafe_dayView *safe_dayV;

@end

@implementation LBSuperMarketHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setProductModel:(LBSuperMarketProducts *)productModel{
    _productModel = productModel;
    
    self.namePriceV.productModel = productModel;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:productModel.img]];
    
//    NSLog(@"%@",productModel);
    //判断是否有优惠
    if (_productModel.pm_desc.length <= 0) {
        [self.captureV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        [self.captureV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.captureV.bounds.size.height);
        }];
        self.captureV.dict = @{@"value":productModel.pm_desc};
    }

    //判断是否有品牌
    if (_productModel.brand_name.length <= 0) {
        [self.brandV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        [self.brandV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.brandV.bounds.size.height);
        }];
        self.brandV.dict = @{@"value":productModel.brand_name};
    }
    
    //判断是否有产品规格
    if (_productModel.specifics.length <= 0) {
        [self.specificsV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        [self.specificsV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.specificsV.bounds.size.height);
        }];
        self.specificsV.dict = @{@"value":productModel.specifics};
        
//        NSLog(@"%@",self.brandV.dict);
    }
    
    //判断是否有保质期限
    if ([_productModel.safe_day intValue] > 0) {
        [self.safe_dayV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.safe_dayV.bounds.size.height);
        }];
        self.safe_dayV.dict = @{@"value":productModel.safe_day};
    }else{
//        [self.safe_dayV mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(0);
//        }];
        [self.safe_dayV removeFromSuperview];
    }

}

- (instancetype)init{
    if (self = [super init]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    
    UIImageView *iconView = [[UIImageView alloc]init];
    self.iconView = iconView;
    iconView.backgroundColor = [UIColor redColor];
    [self addSubview:iconView];
    
    LBNamePriceView *namePriceV = [LBNamePriceView namePriceView];
    self.namePriceV = namePriceV;
    [self addSubview:namePriceV];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 384));
        make.top.offset(0);
        make.left.offset(0);
    }];
    
    [namePriceV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 77));
        make.left.offset(0);
        make.top.mas_equalTo(iconView.mas_bottom);
    }];
    
    LBCaptureView *captureV = [LBCaptureView captureView];
    self.captureV = captureV;
    [self addSubview:captureV];
    
    [captureV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(namePriceV.mas_bottom);
        make.height.mas_equalTo(captureV.bounds.size.height);
    }];
    
    LBBrandView *brandV = [LBBrandView brandView];
    self.brandV = brandV;
    [self addSubview:brandV];
    
    [brandV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(captureV.mas_bottom);
        make.height.mas_equalTo(brandV.bounds.size.height);
    }];

    LBSpecificsView *specificsV = [LBSpecificsView specificsView];
    self.specificsV = specificsV;
    [self addSubview:specificsV];
    
    [specificsV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(brandV.mas_bottom);
        make.height.mas_equalTo(specificsV.frame.size.height);
    }];
    
    LBSafe_dayView *safe_dayV = [LBSafe_dayView safe_dayView];
    self.safe_dayV = safe_dayV;
    [self addSubview:safe_dayV];
    
    [safe_dayV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(specificsV.mas_bottom);
        make.height.mas_equalTo(safe_dayV.bounds.size.height);
        //        make.bottom.offset(0);
    }];
    
//    [self mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(kScreenWidth);
//        make.bottom.equalTo(safe_dayV);
//    }];

}


@end
